home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4439 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.1 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: why not add getch() to the std C RTL?
  5. Date: 4 Feb 1996 08:54:38 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4f2oceINNmdj@keats.ugrad.cs.ubc.ca>
  8. References: <DM85L8.5Jn@emr1.emr.ca> <bnelsonDM887t.LDI@netcom.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <bnelsonDM887t.LDI@netcom.com>,
  12. Bob Nelson <bnelson@netcom.com> wrote:
  13. >On Sun, 4 Feb 1996 00:05:32 GMT, John Grant wrote:
  14. >
  15. >> There are *many* people who want to do the following in their C programs:
  16. >> - read a single unbuffered keystroke (getch() in DOS, ioctl in unix...)
  17. >> The usual response is "not standard C, o/s-specific" etc.
  18. >
  19. >I'll just quote the following from "Expert C Programming" by Peter van
  20. >der Linden (ISBN 0-13-177429-8) and offer no comments of my own, other
  21. >than some emphasis added.  Instead, it'll be interesting to see how
  22. >participants in comp.lang.c respond to what PvdL thinks (especially in
  23. >the context of your posting). Here's the paragraph from page 213:
  24. >
  25. >"People often wonder why ANSI C didn't define a standard function to
  26. >get a character if a key has been pressed. Without a standard
  27. >function, every system has a different method and program portability
  28. >is lost.  The argument against providing kbhit() as part of the
  29. >standard is that *it is mostly useful for GAMES software* and there
  30. >are many other terminal I/O features that are not standardized. In
  31. >addition, you don't want to promise a standard library function *that
  32. >SOME OS's will find difficult to provide.* The argument for providing
  33. >it is that it is mostly useful for games software and that games
  34. >writers don't need the myriad of other terminal I/O features that
  35. >could be standardized. Whichever view you hold, it's true the XJ311
  36. >missed an opportunity to reinforce C as the language of choice for a
  37. >generation of programmers writings games on UNIX."
  38. >
  39. >Comments?
  40.  
  41. That's a little bizarre.
  42.  
  43. I mean, someone isn't going to be discouraged from programming games just
  44. because he or she isn't handed a kbhit() library function on a silver platter.
  45. I don't see how including such a function would reinforce C among UNIX games
  46. programmers (reinforce versus what other popular systems programming language
  47. that DOES provide such a function?)
  48.  
  49. I'd say that the lack of such a function discourages inexperienced programmers
  50. from writing terminal-based programs altogether, and has little to do with
  51. reinforcing the C language.
  52.  
  53. It certainly did not stop some good games from being developed (nethack, hunt
  54. and so forth).
  55.  
  56. Still, a function like kbhit() is simply inappropriate for UNIX. There is more
  57. than One Way (TM) of doing things. You can write games without requiring such a
  58. function. I recently "fixed up" a terminal-based Tetris game which had somewhat
  59. broken terminal interaction.
  60.  
  61. In my fix, I used completely blocking reads from the "keyboard". The
  62. asynchronous advance of the blocks is triggered by a timer interrupt, so I
  63. never have to check whether or not the keyboard contains a character.
  64.  
  65. Another way to check is to use non-blocking I/O. A third way is to use a
  66. special terminal input mode that waits for a specified number of cahracters or
  67. a timeout, whicever occurs first. Yet another way is to poll the input file
  68. descriptor (along with possibly others) using select(). All methods require, in
  69. order to detect individual characters, that the terminal be put into cbreak or
  70. fully raw mode first. A kbhit() implementation would have to also include
  71. routines for entering and leaving this raw mode.
  72.  
  73. Which method of input would kbhit() choose? Non-blocking I/O, special terminal
  74. nput mode, select() or timer interrupts? Each has relative advantages and
  75. disadvantages that depend on the nature of the application and are best left to
  76. the programmer thereof.
  77.  
  78. In any case, this has strayed from the topic coverage of "comp.lang.c" quite
  79. far already!
  80.  
  81. But I do hope that it illustrates why some functions are not as easily made
  82. part of portability standard that aims for a very broad scope.
  83. -- 
  84.  
  85.